//
//  AY_SignTaskTableViewCell.m
//  AiYue
//
//  Created by xyh on 2025/1/19.
//

#import "AY_SignTaskTableViewCell.h"

@interface AY_SignTaskTableViewCell ()
@property (nonatomic,strong) UIView * bg_View;//底部圆角

/** 标题  */
@property (strong,nonatomic) UILabel * topTitle;
@property (nonatomic, strong) UILabel * taskTitle;
@property (nonatomic, strong) UILabel * taskDetailTitle;
@property (nonatomic, strong) UILabel * taskAwardLab;
@property (nonatomic, strong) UIButton * goFinishBtn;
@property (nonatomic, strong) UIView *lineView;

@end
@implementation AY_SignTaskTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        [self addAllSubviewsWithUI];
    }
    return self;
}

- (void)addAllSubviewsWithUI {
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(4) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.height.mas_equalTo(YNUM(120));
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(YNUM(16));
    }];
    
    
    self.topTitle = [AYTool createLabelWithFrame:CGRectZero andText:@"做任务赚金币" andTextColor:COLOR(@"#010101") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(16));
        make.left.equalTo(self.bg_View).offset(YNUM(14));
        make.height.mas_equalTo(YNUM(26));
    }];
    
    self.taskTitle = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#010101") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.taskTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(60));
        make.left.equalTo(self.bg_View).offset(YNUM(14));
        make.height.mas_equalTo(YNUM(22));
    }];
    
    self.taskDetailTitle = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#909090") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.taskDetailTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.taskTitle.mas_bottom).offset(YNUM(2));
        make.left.equalTo(self.taskTitle);
        make.height.mas_equalTo(YNUM(20));
    }];
    
    self.taskAwardLab = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#F86923") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.taskAwardLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.taskTitle.mas_right).offset(YNUM(14));
        make.centerY.equalTo(self.taskTitle);
    }];
    
    self.goFinishBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"" andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andTitleColor:COLOR(@"#FFFFFF") andBg:COLOR(@"#F86923") andImage:nil andBgImage:nil andTarget:self andAction:@selector(goFinishWithAction) andRadius:YNUM(14) andAddSubView:self.bg_View];
    [self.goFinishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bg_View).offset(-YNUM(14));
        make.top.equalTo(self.taskTitle.mas_top).offset(YNUM(8));
        make.height.mas_equalTo(YNUM(28));
        make.width.mas_equalTo(YNUM(75));
    }];
    
    self.lineView = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"#EFEFEF") andRadius:0 andAddSubView:self.bg_View];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.bg_View);
            make.centerX.equalTo(self.bg_View);
            make.left.equalTo(self.bg_View).offset(YNUM(14));
            make.height.mas_equalTo(1);
    }];
}

- (void)setTaskModel:(AY_TaskListModel *)taskModel
{
    _taskModel = taskModel;
    self.taskTitle.text = taskModel.taskName;
    self.taskDetailTitle.text = taskModel.tipText;
    self.taskAwardLab.text = taskModel.anchorText?taskModel.anchorText:@"";
    if([taskModel.isFist intValue] == 1){
        self.topTitle.hidden = NO;
        self.lineView.hidden = NO;
        [self.bg_View mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(YNUM(120));
        }];
        [self.taskTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bg_View).offset(YNUM(60));
        }];
        [self.bg_View layoutIfNeeded];
        [AYTool cornerView:self.bg_View andWithRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight ornerRadii:CGSizeMake(YNUM(8), YNUM(8))];
        NSLog(@"-----------%@",NSStringFromCGRect(self.bg_View.frame));
    }else if ([taskModel.isLast intValue] == 1){
        self.topTitle.hidden = YES;
        self.lineView.hidden = YES;
        [self.bg_View mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(YNUM(72));
        }];
        [self.taskTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bg_View).offset(YNUM(12));
        }];
        [AYTool cornerView:self.bg_View andWithRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight ornerRadii:CGSizeMake(YNUM(8), YNUM(8))];
    }else{
        self.topTitle.hidden = YES;
        self.lineView.hidden = NO;
        [self.bg_View mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(YNUM(72));
        }];
        [self.taskTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bg_View).offset(YNUM(12));
        }];
        [AYTool cornerView:self.bg_View andWithRoundingCorners:UIRectCornerAllCorners ornerRadii:CGSizeMake(YNUM(0), YNUM(0))];
    }
    
    if([taskModel.taskType isEqualToString:@"ggsp"]){
        // 看视频
        [self setFinishStyleWithTitle:@"立即领取"];
    }else if ([taskModel.taskType isEqualToString:@"ydjl"]){
        // 阅读赚金币
        [self setFinishStyleWithTitle:@"去阅读"];
    }else if ([taskModel.taskType isEqualToString:@"tz"]){
        // 限时兑换
        [self setFinishStyleWithTitle:@"去兑换"];
    }
    
}

- (void)setFinishStyleWithTitle:(NSString *)title
{
    if([self.taskModel.finished isEqualToString:@"0"]){
        // 没有完成
        [self.goFinishBtn setTitle:title forState:UIControlStateNormal];
        self.goFinishBtn.enabled = YES;
        if ([self.taskModel.taskType isEqualToString:@"ggsp"]) {
            if ([self.taskModel.isSelect intValue] == 0) {
                self.goFinishBtn.enabled = YES;
            }else{
                self.goFinishBtn.enabled = NO;
            }
            [self.goFinishBtn setTitleColor:COLOR(@"#FFFFFF") forState:UIControlStateNormal];
            [self.goFinishBtn setBackgroundColor:COLOR(@"#F86923")];
        }else{
            [self.goFinishBtn setTitleColor:COLOR(@"#F86923") forState:UIControlStateNormal];
            [self.goFinishBtn setBackgroundColor:[UIColor colorWithRed:248/255.0 green:105/255.0 blue:35/255.0 alpha:0.1000]];
        }
    }else{
        if ([self.taskModel.taskType isEqualToString:@"ggsp"]) {
            // 已领取
            [self.goFinishBtn setTitle:@"已领取" forState:UIControlStateNormal];
            [self.goFinishBtn setTitleColor:COLOR(@"#767676") forState:UIControlStateNormal];
            [self.goFinishBtn setBackgroundColor:COLOR(@"#EFEFEF")];
            self.goFinishBtn.enabled = NO;
        }else{
            if([self.taskModel.rewardAcquired isEqualToString:@"0"]){
                [self.goFinishBtn setTitle:@"立即领取" forState:UIControlStateNormal];
                [self.goFinishBtn setTitleColor:COLOR(@"#FFFFFF") forState:UIControlStateNormal];
                [self.goFinishBtn setBackgroundColor:COLOR(@"#F86923")];
                self.goFinishBtn.enabled = YES;
            }else{
                // 已领取
                [self.goFinishBtn setTitle:@"已领取" forState:UIControlStateNormal];
                [self.goFinishBtn setTitleColor:COLOR(@"#767676") forState:UIControlStateNormal];
                [self.goFinishBtn setBackgroundColor:COLOR(@"#EFEFEF")];
                self.goFinishBtn.enabled = NO;
            }
        }
    }
}

- (void)goFinishWithAction {
    if(self.finishBlock){
        self.finishBlock(self.taskModel);
    }
}



@end
